var wrongImg="<img src='/html/images/valid/wrong.gif' align='absmiddle' />";
var rightImg="<img src='/html/images/valid/right.gif' align='absmiddle' />";
var loading = new Image();
loading.src="/html/images/loadingAnimation.gif";
$(document).ready(
	function(){
		$($('input[name="btypeid"]').get(0)).attr('checked','true');
		$('input[name="useryz"]').focus(
			function(){
				$('#yzImg').attr('src','auth-confirmcode?random='+Math.random());
				$('#yanzhengDiv').show('fast');
			}
		);		
		checkRegister();
		$("input[name='submit']").click(
				function(){
					commonCheck.checknickname($("input[name='nick_name']"));
					commonCheck.checkUserpass($("#registForm input[name='userpass']"));
					commonCheck.checkUserpass2($("input[name='userpass2']"));
					commonCheck.checkEmail($("input[name='email']"));
					commonCheck.checkEmailConfirm("input[name='email_confirm']");
					commonCheck.checkUseryz($("input[name='useryz']"));
					var flag=1;
					$('#registForm li .inputbox span').each(
						function(){							
							if($(this).attr('ok')!='ok'){
								flag=0;										
								return false;
							}
						}
					);	
					if(flag==1){
						GGSHOP.loading.show();
						return true;
					}else{
						return false;
					}
				}
		);
	}
);


function checkRegister(){
	$("input[name='nick_name']").unbind();
	$("input[name='nick_name']").blur(
		function(){
			commonCheck.checknickname(this);
		}
	); 
	$("#registForm input[name='userpass']").unbind();
	$("#registForm input[name='userpass']").blur(
		function(){
			commonCheck.checkUserpass(this);
		}
	); 
	$("input[name='userpass2']").unbind();
	$("input[name='userpass2']").blur(
		function(){
			commonCheck.checkUserpass2(this);
		}
	); 
	$("input[name='email']").unbind();
	$("input[name='email']").blur(
		function(){
			commonCheck.checkEmail(this);
		}
	);
	$("input[name='email_confirm']").unbind();
	$("input[name='email_confirm']").blur(
		function(){
			commonCheck.checkEmailConfirm(this);
		}
	);
	$("input[name='useryz']").unbind('blur');
	$("input[name='useryz']").blur(
		function(){
			commonCheck.checkUseryz(this);
		}
	);
}

var commonCheck={
	checknickname:function(thisA){
		var exp = new RegExp("^[A-Za-z0-9_@\\-\\s]+$", "i");
		if($(thisA).val().length<3){
			$(thisA).next().html(wrongImg+"At least 3 characters");	
			$(thisA).next().css('color','red');
			$(thisA).next().attr('ok','no');
		}else if(!exp.test($(thisA).val())){
			$(thisA).next().html(wrongImg+" your nick name does not meet the rules");
			$(thisA).next().css('color','red');
			$(thisA).next().attr('ok','no');
		}
		else{
			$(thisA).next().html(rightImg+"Right");
			$(thisA).next().css('color','green');
			$(thisA).next().attr('ok','ok');
		}
	},
	checkUserpass:function(thisA){
		if($(thisA).val().length<5){
			$(thisA).next().html(wrongImg+"At least 5 characters");		
			$(thisA).next().css('color','red');	
			$(thisA).next().attr('ok','no');
		}else{
			$(thisA).next().html(rightImg+"Right");
			$(thisA).next().css('color','green');
			$(thisA).next().attr('ok','ok');
		}
	},
	checkUserpass2:function(thisA){
		if($(thisA).val().length<5||$(thisA).val()!=$("#registForm input[name='userpass']").val()){
			$(thisA).next().html(wrongImg+"Different");
			$(thisA).next().css('color','red');	
			$(thisA).next().attr('ok','no');
		}else{
			$(thisA).next().html(rightImg+"The same to the first");
			$(thisA).next().css('color','green');
			$(thisA).next().attr('ok','ok');
		}
	},
	checkEmail:function(thisA){
		var exp = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", "i");
		if(!exp.test($(thisA).val())){
			$(thisA).next().html(wrongImg+"Wrong Email");
			$(thisA).next().css('color','red');
			$("input[name='email']").next().attr('ok','no');
		}else{
			$.ajax({
				type: "POST",
				url: "/auth-check",
				data: {email:$("input[name='email']").val()},
				success: function(data) {					
					switch(data) {					
						case "1":
							$("input[name='email']").next().html(rightImg+"Right");
							$("input[name='email']").next().css('color','green');
							$("input[name='email']").next().attr('ok','ok');
							break;						
						default:							
							$("input[name='email']").next().html(wrongImg+"Email has been registered");	
							$("input[name='email']").next().css('color','red');
							$("input[name='email']").next().attr('ok','no');
					}
				}			
			});			
		}
	},
	checkEmailConfirm:function(thisA){
		var exp = new RegExp("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", "i");
		if(!exp.test($(thisA).val()) || $(thisA).val()!=$("input[name='email']").val()){
			$(thisA).next().html(wrongImg+"Wrong Email Confirm");
			$(thisA).next().css('color','red');
			$(thisA).next().attr('ok','no');
		}else{
			$(thisA).next().html(rightImg+"Right Email Confirm");
			$(thisA).next().css('color','green');
			$(thisA).next().attr('ok','ok');
		}
	},
	checkUseryz:function(thisA){
		if($(thisA).val()==""){
			$(thisA).next().html(wrongImg+"Enter verify Code");
			$(thisA).next().css('color','red');
			$(thisA).next().attr('ok','no');
		}else{
			$(thisA).next().html(rightImg+"For verifying");
			$(thisA).next().css('color','green');
			$(thisA).next().attr('ok','ok');
		}
	}
};